package com.example.neusoft215gdh.androidpro_21.Model;
import com.example.neusoft215gdh.androidpro_21.Interface.ContestFace;
import com.example.neusoft215gdh.androidpro_21.JavaBean.Contest;
import com.example.neusoft215gdh.androidpro_21.Listener.ContestDetailListener;
import com.example.neusoft215gdh.androidpro_21.Listener.ContestListener;
import com.example.neusoft215gdh.androidpro_21.Service.ContestService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by hp-pc on 2017/10/12.
 */

public class ContestModel implements ContestFace{
    private Retrofit retrofit;
    ContestService contestService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址


    public  ContestModel (){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getContest(String sessionId, int page,final ContestListener listener) {
        contestService=retrofit.create(ContestService.class);
        Call<List<Contest>> call=contestService.getContest(sessionId,page);
        call.enqueue(new Callback<List<Contest>>() {
            @Override
            public void onResponse(Call<List<Contest>> call, Response<List<Contest>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponseContest(response.body());
//                 else {
//                    listener.onFail("onresponse fail");
//                }
            }
            @Override
            public void onFailure(Call<List<Contest>> call, Throwable t) {

            }
        });
    }

    @Override
    public void getMatchById(String id,final ContestDetailListener listener) {
        contestService=retrofit.create(ContestService.class);
        Call<Contest> call=contestService.getMatchById(id);
        call.enqueue(new Callback<Contest>() {
            @Override
            public void onResponse(Call<Contest> call, Response<Contest> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Contest> call, Throwable t) {

            }
        });
    }
}
